﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DCityOfRentCar
    {
        public bool Exists(string cityName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tCityOfRentCar");
            strSql.Append(" where CityName=@CityName");
            MySqlParameter[] parameters = {
					new MySqlParameter("@CityName", MySqlDbType.VarChar,20)
          };
            parameters[0].Value = cityName;
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return false;
            }
            int result = int.Parse(ds.Tables[0].Rows[0][0].ToString());
            return result > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.CityOfRentCarInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tCityOfRentCar(");
            strSql.Append("CityName)");
            strSql.Append(" values (");
            strSql.Append("@CityName)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@CityName", MySqlDbType.VarChar,20)};
            parameters[0].Value = model.CityName;            
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.CityOfRentCarInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tCityOfRentCar set ");
            strSql.Append("CityName=@CityName");            
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@CityName", MySqlDbType.VarChar,20),			 			 
					new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.CityName;            
            parameters[1].Value = model.Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tCityOfRentCar ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
            parameters[0].Value = id;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.CityOfRentCarInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,CityName from tCityOfRentCar ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            Model.CityOfRentCarInfo model = new Model.CityOfRentCarInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CityName"] != null && ds.Tables[0].Rows[0]["CityName"].ToString() != "")
                {
                    model.CityName = ds.Tables[0].Rows[0]["CityName"].ToString();
                }             
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM tcompany");          
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }
        
    }
}
